package org.sixdog.mcsit.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.sixdog.mcsit.service.HttpService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
public class HttpServiceImpl implements HttpService {

    @Value("${dynmap.host}")
    private String host;

    @Override
    //未实现，待实现
    public boolean registerDynmap(String userId, String password) {
        try {
            // 构建请求
            HttpPost httpPost = new HttpPost("http://"+host+"/dynmap/up/register");
            HttpClient httpClient = HttpClientBuilder.create().build();
            // 将请求参数进行封装
            JSONObject requestParam = new JSONObject();
            requestParam.put("j_username",userId);
            requestParam.put("j_password",password);
            requestParam.put("j_verify_password",741851);
            requestParam.put("j_passcode","0000-0000");
            // 以JSON格式向接口发送请求
            StringEntity se = new StringEntity(requestParam.toString(),"utf-8");
            httpPost.setEntity(se);
            httpPost.setHeader("Content-Type","application/json");
            // 发起请求
            HttpResponse execute = httpClient.execute(httpPost);
            if (execute.getStatusLine().getStatusCode()==200){
                return true;
            }
            return false;
        } catch (IOException e) {
            // 可能会发生网络异常
            System.out.println("网络异常");
            //e.printStackTrace();
        }
        return false;
    }

    @Override
    public String logingGetCookie(String jUsername, String jPassword) {
        try {
            // 构建请求
            HttpPost httpPost = new HttpPost("http://"+host+"/dynmap/up/login");
            HttpClient httpClient = HttpClientBuilder.create().build();

            // 将请求参数进行封装
            JSONObject requestParam = new JSONObject();
            requestParam.put("j_username",jUsername);
            requestParam.put("j_password",jPassword);
            // 以JSON格式向接口发送请求
            StringEntity se = new StringEntity(requestParam.toString(),"utf-8");
            httpPost.setEntity(se);
            httpPost.setHeader("Content-Type","application/json");
            // 发起请求
            HttpResponse execute = httpClient.execute(httpPost);
            Header firstHeader = execute.getFirstHeader("Set-Cookie");
            // 获取接口的数据
            JSONObject resultJSON = JSONObject.parseObject(EntityUtils.toString(execute.getEntity(), "utf-8"));
            if (execute.getStatusLine().getStatusCode()==200){
                return firstHeader==null? null:firstHeader.getValue();
            }
            return null;

        } catch (IOException e) {
            // 可能会发生网络异常
            System.out.println("网络异常");
            //e.printStackTrace();
        }
        return null;
    }
}
